package com.fowo.api.sys.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fowo.api.entity.ShopInfo;
import com.fowo.api.service.impl.ShopInfoServiceImpl;
import com.fowo.api.sys.entity.SysUser;
import com.fowo.api.sys.entity.SysUserPermissionShopinfo;
import com.fowo.api.sys.mapper.SysUserPermissionShopinfoMapper;
import com.fowo.api.sys.model.SysDictAllItem;
import com.fowo.api.sys.model.SysUserPermissionShopinfoVo;
import com.fowo.api.sys.service.SysDictService;
import com.fowo.api.sys.service.SysUserPermissionShopinfoService;
import com.fowo.api.sys.service.SysUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 店铺用户授权表服务接口实现
 *
 * @author kancy
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2023-09-20 19:09:53
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SysUserPermissionShopinfoServiceImpl extends ServiceImpl<SysUserPermissionShopinfoMapper, SysUserPermissionShopinfo> implements SysUserPermissionShopinfoService {

    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysDictService sysDictService;
    @Resource
    private ShopInfoServiceImpl shopInfoService;
    //店铺平台
    private static String sysSysUserPermissionShopinfoDict = "SCM00106";

    /**
     * 获取全部仓库权限
     */
    @Override
    public List<SysUserPermissionShopinfo> getAllList(Long id) {
        List<SysUserPermissionShopinfo> result = new ArrayList<>();
        //01 获取对应字典
        List<SysDictAllItem> list = sysDictService.getDictItems(sysSysUserPermissionShopinfoDict);
        if (CollUtil.isEmpty(list)) {
            return result;
        }
        //02 循环字典赋值
        for (SysDictAllItem item : list) {
            List<ShopInfo> shopList = shopInfoService.list(Wrappers.<ShopInfo>lambdaQuery().eq(ShopInfo::getPlatform, item.getItemValue()));
            if (CollUtil.isNotEmpty(shopList)) {
                List<SysUserPermissionShopinfo> sysUserPermissionShopinfo = shopList.stream().map(m -> new SysUserPermissionShopinfo(null, id, m.getPlatform(), m.getId(), m.getShopName(), true)).collect(Collectors.toList());
                result.addAll(sysUserPermissionShopinfo);
            }
        }
        return result;
    }

    /**
     * 获取店铺权限
     *
     * @param id             用户id
     * @param selectPlatform 选择的平台
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<SysUserPermissionShopinfoVo> getList(Long id, String selectPlatform, String keywords) {
        Assert.notNull(id, "用户参数异常!");
        List<SysUserPermissionShopinfoVo> result = new ArrayList<>();
        SysUser sysUser = sysUserService.getById(id);
        if (sysUser != null) {
            //01 获取对应字典
            List<SysDictAllItem> list = sysDictService.getDictItems(sysSysUserPermissionShopinfoDict);
            if (CollUtil.isEmpty(list)) {
                return result;
            } else if (StrUtil.isBlank(selectPlatform)) {
                selectPlatform = CollUtil.getFirst(list).getItemValue();
            }
            //02 循环字典赋值
            for (SysDictAllItem item : list) {
                SysUserPermissionShopinfoVo entity = new SysUserPermissionShopinfoVo(item.getItemValue(), item.getItemText(), 0, CollUtil.newArrayList());
                //02.1 当前选中
                if (StrUtil.isNotEmpty(selectPlatform) && StrUtil.equalsIgnoreCase(selectPlatform, item.getItemValue())) {
                    //02.2 选中的店铺+关键字
                    List<ShopInfo> shopList = shopInfoService.list(Wrappers.<ShopInfo>lambdaQuery().eq(ShopInfo::getPlatform, item.getItemValue()).like(StrUtil.isNotBlank(keywords), ShopInfo::getShopName, keywords));
                    if (CollUtil.isNotEmpty(shopList)) {
                        //02.2 在权限表存在:选中
                        List<Object> ids = this.listObjs(Wrappers.<SysUserPermissionShopinfo>lambdaQuery().eq(SysUserPermissionShopinfo::getUserId, id).eq(SysUserPermissionShopinfo::getPlatform, selectPlatform).select(SysUserPermissionShopinfo::getShopId));
                        List<SysUserPermissionShopinfo> sysUserPermissionShopinfo = shopList.stream().map(m -> new SysUserPermissionShopinfo(null, id, m.getPlatform(), m.getId(), m.getShopName(), ids.contains(m.getId()))).collect(Collectors.toList());
                        entity.setList(sysUserPermissionShopinfo);
                    }
                }
                //02.3 赋值数量(不受关键字影响)
                long count = shopInfoService.count(Wrappers.<ShopInfo>lambdaQuery().eq(ShopInfo::getPlatform, item.getItemValue()));
                entity.setCount(count);
                result.add(entity);
            }
        }
        return result;
    }

    /**
     * 保存仓库权限
     *
     * @param id   用户id
     * @param list 权限列表
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean create(Serializable id, List<SysUserPermissionShopinfo> list) {
        Assert.notNull(id, "用户参数异常!");
        SysUser sysUser = sysUserService.getById(id);
        if (sysUser != null) {
            this.remove(Wrappers.<SysUserPermissionShopinfo>lambdaQuery().eq(SysUserPermissionShopinfo::getUserId, id));
            return this.saveBatch(list);
        }
        return false;
    }
}